const reser = require("../dao/reserveationList_wxq");
//随机函数
function mon(m, n) {
  return Math.floor(Math.random() * (m - n) + n);
}
module.exports = {
  async setuserList(req, res) {
    let { userId } = req.query;
    let sql =
      "select * from patients_with_wxq as a,attendance_card as b where a.patients_id=b.card_user_id and patients_id=?";
    let arr = [userId];
    let hzdata = await reser.setserveation(res, sql, arr);
    let cardId = hzdata[0].card_id;
    let sqle =
      "select * from patients_with_wxq where card_id=? order by patients_default ";
    let arr1 = [cardId];
    let alldata = await reser.setserveation(res, sqle, arr1);
    let data = hzdata[0];
    data.list = alldata;
    res.send({
      code: 200,
      msg: "查询成功",
      data,
    });
  },
  async setkeshi(req, res) {
    let sql = "select * from department_one order by department_one_id";
    let data = await reser.setserveation(res, sql, []);
    res.send({
      code: 200,
      msg: "查询成功",
      data,
    });
  },
  async seterkeshi(req, res) {
    let { id } = req.query;
    let sql =
      "select * from department_two where department_one_id=? order by department_two_id";
    let data = await reser.setserveation(res, sql, [id]);
    res.send({
      code: 200,
      msg: "查询成功",
      data,
    });
  },
  async seterkeshiDoctor(req, res) {
    let { one_id, two_id, date } = req.query;
    one_id = parseInt(one_id);
    two_id = parseInt(two_id);
    let sql =
      "select * from doctor,job_title,scheduls ,department_one,department_two where doctor.one_id=department_one.department_one_id and doctor.two_id=department_two.department_two_id and doctor.rank_id=job_title.jt_id and scheduls.doctor_id=doctor.dt_id and one_id=? and two_id=? and sched_date=? order by dt_id ";
    let arr = [one_id, two_id, date];
    console.log(sql, arr);

    let data = await reser.setserveation(res, sql, arr);
    res.send({
      code: 200,
      msg: "查询成功",
      data,
    });
  },
  async setDoctorPaiban(req, res) {
    let { id, start, end, one_id, two_id } = req.query;
    id = parseInt(id);
    one_id = parseInt(one_id);
    two_id = parseInt(two_id);

    let sql =
      "select * from scheduls where  scheduls.doctor_id=? and sched_date>? and sched_date<=? order by sched_date ";
    let arr = [id, start, end];
    let pbData = await reser.setserveation(res, sql, arr); //医生近期排班
    console.log(sql, arr);

    let sqle =
      "select * from doctor,job_title where doctor.rank_id=job_title.jt_id and one_id=? and two_id=? and dt_id!=? order by dt_id ";
    let arr1 = [one_id, two_id, id];
    let dtData = await reser.setserveation(res, sqle, arr1); //同科室医生

    //随机推荐医生
    let dtdatas = [];
    if (dtData.length > 5) {
      let open = [];
      for (let i = 0; i < 5; i++) {
        let sb = mon(dtData.length, 0);
        if (open.indexOf(sb) == -1) {
          open.push(sb);
          dtdatas.push(dtData[sb]);
        }
      }
    } else {
      dtdatas = dtData;
    }

    //推荐医生排班

    console.log(sqle, arr1, dtdatas, 000);

    res.send({
      code: 200,
      msg: "查询成功",
      pbData,
      dtdatas,
    });
  },
  async addJzhz(req, res) {
    let {
      patients_id,
      doctor_id,
      rt_status,
      startDate,
      startTime,
      creadteDate,
      reser_type,
      endDate,
      id,
      sxw,
      num,
    } = req.body;
    let sql =
      "insert into reservation (rt_id,patients_id,doctor_id,rt_status,startDate,startTime,creadteDate, content,reser_type,endDate,cancel) values(null,?,?,?,?,?,?,null,?,?,null)";
    let arr = [
      patients_id,
      doctor_id,
      rt_status,
      startDate,
      startTime,
      creadteDate,
      reser_type,
      endDate,
    ];
    let sqle;
    if (sxw == "上午") {
      sqle = "UPDATE scheduls SET am_sy=? WHERE doctor_id=? and sched_date=?";
    } else {
      sqle = "UPDATE scheduls SET pm_sy=? WHERE doctor_id=? and sched_date=?";
    }
    let arr1 = [num, id, creadteDate];
    console.log(sqle, arr1, 212312);
    let data = await reser.chageData(res, sql, arr);
    let data1 = await reser.chageData(res, sqle, arr1);
    console.log(data, data1);
    res.send({ code: 200, msg: "成功返回" });
  },
};
